{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from gs_quant.markets.portfolio import Portfolio\n",
    "from gs_quant.instrument import IRSwaption\n",
    "from gs_quant.risk import IRAnnualImpliedVol\n",
    "import pandas as pd\n",
    "import seaborn as sns\n",
    "import matplotlib.pyplot as plt"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from gs_quant.session import GsSession\n",
    "# external users should substitute their client id and secret; please skip this step if using internal jupyterhub\n",
    "GsSession.use(client_id=None, client_secret=None, scopes=('run_analytics',))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "tails = ['1y', '3y', '5y', '10y', '15y', '20y', '25y', '30y']\n",
    "expiries = ['3m', '6m', '9m', '1y', '18m', '2y', '3y', '4y', '5y']\n",
    "pay_rec = 'Pay'\n",
    "ccy = 'EUR'\n",
    "moneyness = 25"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "portfolios = Portfolio([Portfolio([IRSwaption(pay_or_receive=pay_rec, notional_currency=ccy, termination_date=t, \n",
    "                                              expiration_date=e, strike='ATM+{}'.format(moneyness), name=e) for e in expiries], name=t) for t in tails])\n",
    "results = portfolios.calc(IRAnnualImpliedVol)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "frame = results.to_frame('value','portfolio_name_0','instrument_name') * 10000\n",
    "frame"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "plt.subplots(figsize=(12, 8))\n",
    "ax = sns.heatmap(frame, annot=True, fmt='.2f', cmap='coolwarm')\n",
    "ax.set(ylabel='Expiries', xlabel='Tails', title='Implied Vol Grid')\n",
    "ax.xaxis.tick_top()"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.4"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
